<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./canvas.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/jszip/3.6.0/jszip.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
  <style>
    .imgList {
      display: grid;
      grid-gap: 1px;
    }
  </style>
</head>
<body>
  <img id="img01" src="./bg4.jpeg" alt="" />
  <hr>
  <div id="imgList" class="imgList"></div>

  <script>
    // 自动裁切原始图片为指定尺寸的小图片，这里是裁切为160x160大小的正方形图片。
    // 并且将裁切后的小图片打包成zip包可下载
    let targetSize = 160
    let img01 = document.getElementById('img01')
    img01.onload = (e) => {
      // let c01 = document.getElementById('canvas01')
      console.log(e)
      let imgW = img01.clientWidth
      let imgH = img01.clientHeight
      let wc = Math.floor(imgW / targetSize)
      let hc = Math.floor(imgH / targetSize)
      console.log('imgW, imgH', imgW, imgH, wc, hc)
      let c01 = document.createElement('canvas')
      c01.width = targetSize
      c01.height = targetSize
      let ctx01 = c01.getContext('2d')
      let newFragElement = document.createDocumentFragment()

      const zip = new JSZip()
      zip.file("readme.txt", "自动裁剪为小图标\n");
      const imgArr = zip.folder('images')

      for(let i = 0; i < hc; i += 1) {
        for (let j = 0; j < wc; j += 1) {
          ctx01.drawImage(img01, targetSize * j, targetSize * i, targetSize, targetSize, 0, 0, targetSize, targetSize)
          let url01 = c01.toDataURL('image/jpeg', 0.2)  
          let newImage = document.createElement('img')
          newImage.src = url01;
          newFragElement.appendChild(newImage)
          imgArr.file(`image_${i}_${j}.jpg`, url01.slice(22), {base64: true})
        }
      }
      console.log(newFragElement)
      const imgList = document.getElementById('imgList')
      imgList.appendChild(newFragElement)
      let imgListStyle = `grid-template-columns: repeat(${wc}, ${targetSize}px);`
      imgList.setAttribute('style', imgListStyle)

      zip.generateAsync({ type: "blob" }).then(function (content) {
        const blob = content
        const fileName = 'images.zip';
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
      });

      
      
      // let b01 = dataURLToBlob(url01)
      // console.log(b01);
      

      // const file01 = new File([b01], 'img01.png', {type: 'image/png'})
      // const newFormData01 = new FormData()
      // newFormData01.append('file', file01)
      // axios({
      //   method: 'POST',
      //   url: 'http://api-uat-yc.icloudcity.com/fileservice/imgUpload',
      //   data: newFormData01,
      //   headers: {
      //     "Content-Type": "multipart/form-data",
      //     Authorization: "Bearer c5016461-28f1-405b-8bd5-674bbfb6873b"
      //   }
      // }).then(res => {
      //   console.log('res01', res)
      //   d01.src = res.data.data[0]
      // }).catch(err => {
      //   console.log(err)
      // })

    }

      
    
  </script>
</body>
</html>